package vashion.azeroth.api.controller.wukong;

import com.google.common.base.Strings;

import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.common.im.AppKeys;
import vashion.azeroth.common.im.LoginSH256Util;

/**
 * Created by xiaonan on 15/11/23.
 */
public class IMRegistControler extends BaseController {
	public static final LoginSH256Util.ImSignModel nullModel = new LoginSH256Util.ImSignModel();

	/***
	 * 这里是比较简单的只传入了openid，
	 * 在你自己的系统中需要对用户的请求进行权限鉴别， 简单的是用户名和密码校验。
	 * 或者通过accesstoken进行校验
	 * 5:21:30 pm
	 * @param appKey
	 * @param openid
	 * @return
	 */
	@RequestMapping("/imlogin/{appKey}/{openid}")
	@ResponseBody
	public Object imlogin(@PathVariable String appKey, @PathVariable Long openid, @RequestParam(required = false) String callback) {
		LoginSH256Util.ImSignModel rst = null;

		String appSecret = AppKeys.getAppSecret(appKey);
		if (Strings.isNullOrEmpty(appSecret)) {
			rst = nullModel;
		} else {
			rst = LoginSH256Util.getLoginModel(appKey, appSecret, openid);
		}

		if (!Strings.isNullOrEmpty(callback)) {
			return new JSONPObject(callback, rst);
		} else {
			return rst;
		}
	}
}
